package com.analysis.learn;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLiteJDBCDriverConnection {

    private static String prefix = "jdbc:sqlite:D:/stock/sqlite/";

    private static String analyse = "jdbc:sqlite:D:/stock/sqlite/analyse.db";
    public Connection getConnection() {

        Connection conn = null;
        try {
            // db parameters
            String url = prefix + "chinook.db";
            conn = DriverManager.getConnection(url);

            System.out.println("Connection to SQLite has been established.");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        return conn;
    }

    public static void createNewDatabase(String fileName) {

        String url = prefix + fileName;

        try (Connection conn = DriverManager.getConnection(url)) {
            if (conn != null) {
                DatabaseMetaData meta = conn.getMetaData();
                System.out.println("The driver name is " + meta.getDriverName());
                System.out.println("A new database has been created.");
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void createNewTable(String sql) {

        // SQL statement for creating a new table
        // String sql = "CREATE TABLE IF NOT EXISTS warehouses (\n" + " id
        // integer PRIMARY KEY,\n"
        // + " name text NOT NULL,\n" + " capacity real\n" + ");";
        //
        // try (Connection conn = DriverManager.getConnection(url); Statement
        // stmt = conn.createStatement()) {
        // // create a new table
        // stmt.execute(sql);
        // } catch (SQLException e) {
        // System.out.println(e.getMessage());
        // }
    }

    public static void main(String[] args) {
        new SQLiteJDBCDriverConnection().createNewTable(analyse);
    }
}
